package com.example.Activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
import android.widget.AdapterView;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.Utils.HttpHelper;
import com.example.training.R;

import org.apache.http.client.utils.URLEncodedUtils;
import org.apache.http.message.BasicNameValuePair;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedList;
import java.util.List;

public class OnlineDiskSearchActivity extends Activity implements AdapterView.OnItemClickListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.onlinedisksearch_activity);

        _viewOnlineDisks = (GridView) findViewById(R.id.view_onlinedisks);

        ArrayList<HashMap<String, Object>> lstItem = createOnlineDisks();
        _adapter = new SimpleAdapter(this,
                lstItem,
                R.layout.onlinedisksearch_itemstyle,
                new String[] {"radioIcon", "ItemText", "ItemUrl"},
                new int[] {R.id.img_onlinedisk, R.id.text_onlinedisk,R.id.text_onlinediskurl});
        _viewOnlineDisks.setAdapter(_adapter);
        changeItemImg(_lastItem, true);
        _site = "pan.baidu.com";
        _viewOnlineDisks.setOnItemClickListener(this);
    }

    private ArrayList<HashMap<String, Object>> createOnlineDisks(){
        ArrayList<HashMap<String, Object>> list = new ArrayList<HashMap<String, Object>>();
        list.add(createItem("百度网盘", "pan.baidu.com"));
        list.add(createItem("360云盘", "yunpan.cn"));
        list.add(createItem("115网盘", "115.com"));
        list.add(createItem("迅雷快盘", "kuai.xunlei.com"));
        list.add(createItem("金山快盘", "www.kuaipan.cn"));
        list.add(createItem("新浪爱问", "ishare.iask.sina.com.cn"));
        list.add(createItem("百度文库", "wenku.baidu.com"));
        list.add(createItem("华为网盘", "dl.dbank.com"));
        list.add(createItem("CSDN", "download.csdn.net"));
        return list;
    }

    private HashMap<String, Object> createItem(String content, String url){
        HashMap<String, Object> map = new HashMap<String, Object>();
        map.put("ItemText", content);
        map.put("ItemUrl", url);
        map.put("radioIcon", R.drawable.radio_off);
        return map;
    }

    @Override
    public void onItemClick(AdapterView<?> adapterView, View view, int position, long id) {
        if ((_lastItem >= 0) && (_lastItem != position)) {
            changeItemImg(_lastItem, false);
        }
        changeItemImg(position, true);
        HashMap<String, Object> item  = (HashMap<String, Object>) adapterView.getAdapter().getItem(position);
        _site = item.get("ItemUrl").toString();
        _lastItem = position;
    }

    private void changeItemImg(int selectedItem, boolean isOn) {
        HashMap<String, Object> map = (HashMap<String, Object>)_adapter.getItem(selectedItem);
        if (isOn) {
            map.put("radioIcon", R.drawable.radio_on);
        } else {
            map.put("radioIcon", R.drawable.radio_off);
        }
        _adapter.notifyDataSetChanged();
    }


    public void DoSearchOnlineDisk(View view){
        EditText txtFilter =  (EditText) findViewById(R.id.text_file_filter);
        Intent intent = new Intent(this, OnlineDiskResultsActivity.class);
        intent.putExtra("filter", txtFilter.getText().toString());
        intent.putExtra("site", _site);
        startActivity(intent);
    }

    private GridView _viewOnlineDisks;
    private String _site;
    private int _lastItem;
    private SimpleAdapter _adapter;
}
